/**
 * 定义常量
 * @type {{msg: string, data: string, status: string}}
 */

const constant = {
    msg:'msg',
    status:'status',
    data:'data'
};
let wpgaci = {
    goUrl: function (url, mins) {
        setTimeout(function () {
            top.location.href = url;
        }, mins * 1000);
    },
    /**
     * 从上传的Excel文件中获取json数据(默认读取第一张sheet，以第一行为键)
     * @param file
     * @param callback 回调方法
     * @returns {boolean} 是否执行成功
     */
    getJsonFromLocalExcelFile:function (file,callback){
        if(!/\.xlsx$/g.test(file.name)) {
            alert('仅支持读取xlsx格式！');
            return false;
        }
        let reader = new FileReader();
        reader.onload = function(e) {
            let data = e.target.result;
            let workbook = XLSX.read(data, {type: 'binary'});
            let sl = workbook.SheetNames;
            if(sl.length < 1) return false;
            let s = workbook.Sheets[sl[0]]//获取第一个sheet
            let json = [];
            json = json.concat(XLSX.utils.sheet_to_json(s));//把sheet数据转为json数据
            if(typeof callback === "function"){
                callback(json);
            }
        };
        reader.readAsBinaryString(file);
    },
    exportExcelFromArray(arr = [],fileName = 'excel文件名称',sheetName = 'Sheet1'){
        fileName += '.xlsx'; //文件名称
        let wb = XLSX.utils.book_new();
        let ws = XLSX.utils.aoa_to_sheet(arr);
        XLSX.utils.book_append_sheet(wb, ws, sheetName);  //将数据添加到工作薄
        XLSX.writeFile(wb, fileName); //导出Excel
    }
};
